/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     laowang   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "rf_foat_cfg.h"
#include <fal.h>
#include "vconsole.h"

int main(void)
{
#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
    extern int finsh_set_prompt(const char * prompt);
    finsh_set_prompt("rt-fota />");
#endif
    extern void rt_fota_init(void);
    rt_fota_init();

    return RT_EOK;
}

static rt_device_t vcom_shell_device = NULL;
static int change_shell(void)
{
    vcom_shell_device = rt_device_find("vcom");
    if (vcom_shell_device)
    {
        vconsole_switch(vcom_shell_device);
    }
    return RT_EOK;
}
INIT_APP_EXPORT(change_shell);

static int flash_enable_read_protection(void)
{
    FLASH_OBProgramInitTypeDef OBInit;
    __HAL_FLASH_PREFETCH_BUFFER_DISABLE();

    HAL_FLASHEx_OBGetConfig(&OBInit);
    if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
    {
        OBInit.OptionType = OPTIONBYTE_RDP;
        OBInit.RDPLevel = OB_RDP_LEVEL_1;
        HAL_FLASH_Unlock();
        HAL_FLASH_OB_Unlock();
        HAL_FLASHEx_OBProgram(&OBInit);
        HAL_FLASH_OB_Lock();
        HAL_FLASH_Lock();
    }
    __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  return RT_EOK;
}
INIT_COMPONENT_EXPORT(flash_enable_read_protection);
